﻿using Microsoft.Extensions.DependencyInjection;
using SqlSugar;

namespace XieCan.Core.Api
{
  public static class SqlSugarExtension
  {
    public static IServiceCollection AddSqlSugarExt(this IServiceCollection services)
    {
      services.AddSingleton<ISqlSugarClient>(s =>
      {
        return new SqlSugarClient(
        [
           new ConnectionConfig()
           {
             ConfigId = "0",
             DbType = DbType.MySql,
             ConnectionString = "server=localhost;Database=ApiTest;Uid=root;Pwd=root;",
             IsAutoCloseConnection = true,
             ConfigureExternalServices = new ConfigureExternalServices
             {
               EntityNameService = (type, entity) =>
               {
                 //entity.DbTableName = entity.DbTableName.Replace("Entity", "").ToLower();//转小写
               },
               //EntityService = (type, column) =>
               //{
               //  column.DbColumnName = column.DbColumnName.ToLower();//转小写
               //}
             }
           }
        ],
         db =>
         {
           //如果是GetConnectionScope或者GetConnectionScopeWithAttr这边也应该是GetConnectionScope
           //如果用的是GetConnection或者GetConnectionWithAttr这边也应该是GetConnection
           db.GetConnectionScope("0").Aop.OnLogExecuting = (sql, p) =>
           {
             Console.WriteLine(sql);
           };
         });
      });
      return services;
    }
  }
}